import { message } from 'antd'
import moment from 'moment'
import router from 'umi/router'
import { queryProject, queryProfile, projectNew, projectEdit, projectStaffNew, projectStaffRemove } from '../services/project'

export default {
  namespace: 'projectHall',

  state: {
    visible: false,
    isProfile: false,
    profile: {},

    initLoading: true,
    loading: false,
    isDisable: false,
    data: [],
    list: [],
  },

  effects: {
    // 获取项目列表
    * fetchProject({ payload = {} }, { call, put, select }) {
      const projectHall = yield select(_ => _.projectHall)

      yield put({
        type: 'save',
        payload: {
          loading: true,
          initLoading: false,
          list: projectHall.data.concat([...new Array(3)].map(() => ({ loading: true, name: {} }))),

          // eslint-disable-next-line
          project_name: payload.project_name ? payload.project_name : projectHall.project_name ? projectHall.project_name : null
        },
      })

      // 搜索
      if (projectHall.project_name && !payload.project_name) {
        Object.assign(payload, {
          project_name: projectHall.project_name
        })
      }

      const { status = false, data = [] } = yield call(queryProject, payload)
      if (status) {
        const list = projectHall.data.concat(data)
        yield put({
          type: 'save',
          payload: { data: list, list, loading: false, },
        })
      } else {
        yield put({
          type: 'save',
          payload: { ...projectHall, loading: false, isDisable: true },
        })
      }
      // console.log('1111111111111111111111111111111111111111111111111111111111111111111111111')
      window.dispatchEvent(new Event('resize'))
    },
    // 获取项目详情
    * fetchProfile({ payload = {} }, { call, put, /* select */ }) {
      const { status = false, data = {} } = yield call(queryProfile, {
        project_id: payload.project_id
      })

      if (status) {
        // eslint-disable-next-line
        const { project_id = '', project_name = '', project_start = '', project_end = '' } = data
        let { task = [], file } = data

        task = task.map(it => ({
          // 格式化
          task_id: it.task_id,
          department: it.department_id,
          start: it.task_start,
          end: it.task_end,
          staff: it.staff,
          // 页面需要
          staff_name: it.staff_name,
        }))
        file = file.map(it => ({
          // 格式化
          name: it.file_name,
          url: it.file_url
        }))

        const profile = {
          // 格式化
          project_id,
          name: project_name,
          start: project_start,
          end: project_end,
          task,
          file
        }

        yield put({
          type: 'save',
          payload: {
            ...payload,
            profile
          },
        })
      }
    },
    // 新增项目成员
    * postStaff({ payload = {} }, { call, put, select }) {
      const { profile } = yield select(_ => _.projectHall)
      const { status } = yield call(projectStaffNew, payload)
      if (status) {
        message.success('新增项目员工成功')
        yield put({
          type: 'fetchProfile',
          payload: { visible: true, project_id: profile.project_id }
        })
        yield put({
          type: 'fetchProject'
        })
        router.push('/pro/search')
      } else {
        message.error('新增项目员工失败')
      }
    },
    // 删除项目成员
    * removeStaff({ payload = {} }, { call, put }) {
      const { status } = yield call(projectStaffRemove, payload)
      if (status) {
        message.success('删除项目员工成功')
        yield put({
          type: 'fetchProject'
        })
        router.push('/pro/search')
      } else {
        message.error('删除项目员工失败')
      }
    },
    // 提交详情
    * handleSubmit({ payload = {} }, { call, put }) {
      // eslint-disable-next-line
      const { project_id, name, start, end } = payload
      let { task, file } = payload

      task = task.map(i => ({
        department: i.department,
        staff: i.staff || '详情的时候没传给 所以这样',
        start: moment(i.start).format('YYYY-MM-DD HH:mm:ss'),
        end: moment(i.end).format('YYYY-MM-DD HH:mm:ss')
      }))
      file = file.map(i => ({
        name: i.name,
        url: i.url
      }))

      const params = {
        start: moment(start).format('YYYY-MM-DD HH:mm:ss'),
        end: moment(end).format('YYYY-MM-DD HH:mm:ss'),
        name,
        task,
        file
      }
      let uri = null
      // eslint-disable-next-line
      if (project_id) {
        uri = projectEdit
        Object.assign(params, { project_id })
      } else {
        uri = projectNew
      }
      const { status } = yield call(uri, params)
      if (status) {
        message.success('提交成功')
        yield put({
          type: 'save',
          payload: {
            visible: false
          },
        })
        router.push('/pro/search')
        yield put({
          type: 'fetchProject'
        })
      } else {
        message.error('提交失败')
      }
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(() => {
        // 路由发生改变时 清空数据
        dispatch({
          type: 'save',
          payload: {
            list: [],
            data: [],
            isDisable: false,
            project_name: null
          },
        })
      })
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
  },
}